package org.stone.project.webIntegration.filter;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.util.IoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.stone.project.common.pojo.constant.SysConstant;
import org.stone.project.common.pojo.constant.WebFilterOrder;
import org.stone.project.common.utils.JsonUtil;
import org.stone.project.logInterface.pojo.dto.HttpLog;
import org.stone.project.logInterface.service.LogUserInfoService;
import org.stone.project.logInterface.utils.LogUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 日志过滤器
 */
@Component
@Order(WebFilterOrder.LOG)
@ConditionalOnProperty(name = "logging.filter.enabled",havingValue = "true", matchIfMissing = false)
public class LogFilter implements Filter {
    @Autowired(required = false)
    private LogUserInfoService logUserInfoService;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        String url = httpRequest.getRequestURI();
        if (servletRequest instanceof HttpBodyRepeatReadFilter.MyRequestWrapper){
            HttpBodyRepeatReadFilter.MyRequestWrapper myWrapper = (HttpBodyRepeatReadFilter.MyRequestWrapper) servletRequest;
            if (myWrapper.getCacheFlag()){
                LogUtil.addSysInfoLog("请求信息为{}", JsonUtil.toJson(new HttpLog(url,new String(myWrapper.getBody(),SysConstant.UTF8))));
            }
        }

        filterChain.doFilter(httpRequest, httpResponse);

        if (httpResponse instanceof HttpBodyRepeatReadFilter.MyResponseWrapper){
            HttpBodyRepeatReadFilter.MyResponseWrapper myWrapper = (HttpBodyRepeatReadFilter.MyResponseWrapper) httpResponse;
            if (myWrapper.getCacheFlag()){
                byte[] content = null;
                try(
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        ByteArrayInputStream bis = ((HttpBodyRepeatReadFilter.MyResponseWrapper) (httpResponse)).getDataStream();
                        ){
                    IoUtils.copy(bis,bos);
                    content = bos.toByteArray();
                }
                HttpLog logResp = new HttpLog(url, new String(content, SysConstant.UTF8));
                if (ObjectUtil.isNotNull(logUserInfoService)){
                    String userInfo = SysConstant.EMPTY_STR;
                    try {
                        userInfo = logUserInfoService.getUserPrimaryInfo();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    logResp.setUserInfo(userInfo);
                }
                LogUtil.addSysInfoLog("响应信息为{}",JsonUtil.toJson(logResp));
            }
        }
    }
}
